from django.core.mail import send_mail
from celery_tasks.main import celery_app
from django.conf import settings
import eventlet

# bind:　保证task对象第一个参数传入
# name:　任务异步别名
# retry_backoff:　异步自动重试的时间间隔，　第n次 (retry_backoffx2^(n-1))
# max_retry: 最大重试次数
# @celery_app.task(name="send_verify_email")
@celery_app.task(bind=True, name="send_verify_email", retry_backoff=3)
def send_verify_email(self, to_email, verify_url):
    """定义发送邮箱的任务"""
    # send_mail(标题，普通邮件正文，发件人，收件人列表，富文本文件内容(html))
    subject = "美多商城"
    html_message = '<p>尊敬的用户您好</p>' \
                   '<p>感谢您使用美多商城</p>' \
                   '<p>您的邮箱为%s，请点击激活您的邮箱</p>'\
                   '<p><a href="%s">%s</a></p>' % (to_email, verify_url, verify_url)
    try:
        send_mail(subject, "", settings.DEFAULT_FROM_EMAIL, [settings.DEFAULT_FROM_EMAIL], html_message=html_message)    # fail_silently = False 邮箱是否加密
    except Exception as e:
        # 触发错误重试3次
        raise self.retry(exec=e, max_retries=3)